/*******************************************************************************
 * Copyright (c) 2025 Lablicate GmbH.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 * Matthias Mailänder - initial API and implementation
 *******************************************************************************/
package org.eclipse.chemclipse.msd.filter.supplier.normalizer.core;

import java.util.List;

import org.eclipse.chemclipse.chromatogram.filter.result.ResultStatus;
import org.eclipse.chemclipse.chromatogram.msd.filter.core.massspectrum.AbstractMassSpectrumFilter;
import org.eclipse.chemclipse.chromatogram.msd.filter.result.IMassSpectrumFilterResult;
import org.eclipse.chemclipse.chromatogram.msd.filter.result.MassSpectrumFilterResult;
import org.eclipse.chemclipse.chromatogram.msd.filter.settings.IMassSpectrumFilterSettings;
import org.eclipse.chemclipse.msd.filter.supplier.normalizer.MassSpectrumModifier;
import org.eclipse.chemclipse.msd.filter.supplier.normalizer.settings.AveragesType;
import org.eclipse.chemclipse.msd.filter.supplier.normalizer.settings.MassSpectrumFilterSettings;
import org.eclipse.chemclipse.msd.model.core.IIon;
import org.eclipse.chemclipse.msd.model.core.IScanMSD;
import org.eclipse.chemclipse.processing.core.IProcessingInfo;
import org.eclipse.chemclipse.processing.core.ProcessingInfo;
import org.eclipse.core.runtime.IProgressMonitor;

public class MassSpectrumFilter extends AbstractMassSpectrumFilter {

	@Override
	public IProcessingInfo<IMassSpectrumFilterResult> applyFilter(List<IScanMSD> massSpectra, IMassSpectrumFilterSettings filterSettings, IProgressMonitor monitor) {

		IProcessingInfo<IMassSpectrumFilterResult> processingInfo = validate(massSpectra, filterSettings);
		if(processingInfo.hasErrorMessages()) {
			return processingInfo;
		}
		if(filterSettings instanceof MassSpectrumFilterSettings massSpectrumFilterSettings) {
			for(IScanMSD massSpectrum : massSpectra) {
				return normalize(massSpectrum, massSpectrumFilterSettings);
			}
		}
		return processingInfo;
	}

	private IProcessingInfo<IMassSpectrumFilterResult> normalize(IScanMSD massSpectrum, MassSpectrumFilterSettings massSpectrumFilterSettings) {

		IProcessingInfo<IMassSpectrumFilterResult> processingInfo = validate(massSpectrum, massSpectrumFilterSettings);
		if(processingInfo.hasErrorMessages()) {
			return processingInfo;
		}
		processingInfo = validate(massSpectrum.getIons());
		if(processingInfo.hasErrorMessages()) {
			return processingInfo;
		}

		try {
			if(massSpectrumFilterSettings.getAveragesType() == AveragesType.MEAN) {
				MassSpectrumModifier.meanNormalize(massSpectrum);
			} else if(massSpectrumFilterSettings.getAveragesType() == AveragesType.MEDIAN) {
				MassSpectrumModifier.medianNormalize(massSpectrum);
			}
			massSpectrum.setDirty(true);
			processingInfo.setProcessingResult(new MassSpectrumFilterResult(ResultStatus.OK, "The mass spectrum was successfully normalized."));
		} catch(IllegalArgumentException | IllegalStateException e) {
			processingInfo.setProcessingResult(new MassSpectrumFilterResult(ResultStatus.EXCEPTION, e.getMessage()));
		}
		return processingInfo;
	}

	private IProcessingInfo<IMassSpectrumFilterResult> validate(List<IIon> ions) {

		IProcessingInfo<IMassSpectrumFilterResult> processingInfo = new ProcessingInfo<>();
		processingInfo.addMessages(validateIons(ions));
		return processingInfo;
	}

	private IProcessingInfo<?> validateIons(List<IIon> ions) {

		IProcessingInfo<IMassSpectrumFilterResult> processingInfo = new ProcessingInfo<>();
		if(ions == null || ions.size() == 0) {
			processingInfo.addErrorMessage("Normalizer", "There are no signals stored.");
		}
		return processingInfo;
	}
}
